package com.manor.common.activity;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import androidx.annotation.Nullable;

import com.manor.common.utils.KLog;
import com.manor.common.utils.LocationUtil;

public class BaseLocationActivity extends AbsActivity {

    public static int npcCode = 0;//城市编码

    public static String parentSdNo = "";//上级邀请码

    //    String permissions[] = {
    //            Manifest.permission.READ_EXTERNAL_STORAGE,
    //            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    //            Manifest.permission.CAMERA,
    //            Manifest.permission.RECORD_AUDIO,
    //            Manifest.permission.ACCESS_COARSE_LOCATION
    //
    //
    //    };
    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getLocation();

    }

    @Override
    protected void main() {
        super.main();

    }


    public void getLocation() {
        if (isLocServiceEnable(this)) {
            KLog.e("定位", "开启定位 开始定位");
            LocationUtil.getInstance().startLocation(new LocationUtil.IGetLocationListener() {
                @Override
                public void Success(int cttyId) {
                    npcCode = cttyId;
                    KLog.e("tag", cttyId + "");
                }
            });
            LocationUtil.getInstance().setNeedPostLocationEvent(true);
        } else {
            Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(locationIntent, 10);
        }
    }

    public interface ILocationResultListener {
        void onSuccess(int cityCode);
    }

    public void getLocation(final ILocationResultListener callback) {
        if (isLocServiceEnable(this)) {
            if (npcCode >= 0) {
                if (callback != null) {
                    callback.onSuccess(npcCode);
                }
                return;
            }

            LocationUtil.getInstance().startLocation(new LocationUtil.IGetLocationListener() {
                @Override
                public void Success(int cttyId) {
                    if (callback != null) {
                        if (cttyId >= 0) {
                            callback.onSuccess(cttyId);
                        } else {
                            callback.onSuccess(330108);
                        }
                    }
                    npcCode = cttyId;
                    KLog.e("tag", cttyId + "");
                }
            });
            LocationUtil.getInstance().setNeedPostLocationEvent(true);
        } else {
            Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(locationIntent, 10);
        }

    }


    /**
     * 手机是否开启位置服务，如果没有开启那么所有app将不能使用定位功能
     */
    public static boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        return gps || network;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            //            case REQUEST_CODE_SETTING: {
            //                getProcess();
            //                break;
            //            }
            case 10:
                getLocation();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
